package com.bysj.sausys.service.impl;

import com.bysj.sausys.beans.PageQuery;
import com.bysj.sausys.beans.PageResult;
import com.bysj.sausys.beans.Pagination;
import com.bysj.sausys.dao.SysSauCadreMapper;
import com.bysj.sausys.exception.ParamException;
import com.bysj.sausys.model.SysSauCadre;
import com.bysj.sausys.service.ISysSauCadreService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 社联干部Service
 *
 * @author WangZK
 */
@Service("sysSauCadreService")
public class SysSauCadreServiceImpl implements ISysSauCadreService {

    @Resource
    private SysSauCadreMapper sysSauCadreMapper;

    /**
     * 保存社联干部信息
     *
     * @param sysSauCadre
     * @param username
     */
    @Override
    public void save(SysSauCadre sysSauCadre, String username) {
        //进行数据校验
        if (checkSauCadreExist(sysSauCadre.getId(), sysSauCadre.getStuNum())) {
            throw new ParamException("学号已存在，保存失败");
        }

        sysSauCadre.setInsertMan(username);
        sysSauCadre.setInsertTime(new Date());
        sysSauCadreMapper.insertSelective(sysSauCadre);
    }

    /**
     * 判断社联干部是否已经存在
     *
     * @param id
     * @param stuNum
     * @return
     */
    private boolean checkSauCadreExist(Integer id, String stuNum) {
        return sysSauCadreMapper.countByStuNum(id, stuNum) > 0;
    }

    /**
     * 更新社联干部信息
     *
     * @param sysSauCadre
     * @param username
     */
    @Override
    public void update(SysSauCadre sysSauCadre, String username) {
        //进行数据校验
        SysSauCadre before = sysSauCadreMapper.selectByPrimaryKey(sysSauCadre.getId());
        if (before == null) {
            throw new ParamException("待更新的干部信息不存在");
        }
        if (checkSauCadreExist(sysSauCadre.getId(), sysSauCadre.getStuNum())) {
            throw new ParamException("学号已存在，更新失败");
        }

        SysSauCadre after = new SysSauCadre();

        after.setId(sysSauCadre.getId());
        after.setStuNum(sysSauCadre.getStuNum());
        after.setName(sysSauCadre.getName());
        after.setSex(sysSauCadre.getSex());
        after.setAnnual(sysSauCadre.getAnnual());
        after.setCollege(sysSauCadre.getCollege());
        after.setMajor(sysSauCadre.getMajor());
        after.setDept(sysSauCadre.getDept());
        after.setPosition(sysSauCadre.getPosition());
        after.setSanction(sysSauCadre.getSanction());
        after.setRemarks(sysSauCadre.getRemarks());
        after.setStatus(before.getStatus());
        after.setLastupdMan(username);
        after.setLastupdTime(new Date());

        sysSauCadreMapper.updateByPrimaryKeySelective(after);
    }

    /**
     * 分页获取社联干部列表
     *
     * @param keyword
     * @param pageQuery
     * @return
     */
    @Override
    public PageResult<Map> getPageByKeyword(String keyword, PageQuery pageQuery) {
        int count = sysSauCadreMapper.countByKeyword(keyword);
        if (count > 0) {
            List<Map> list = sysSauCadreMapper.getPageByKeyword(keyword, pageQuery);
            PageResult<Map> result = new PageResult<>();
            result.setList(list);
            Pagination pagination = new Pagination();
            pagination.setTotal(count);
            pagination.setPageSize(pageQuery.getPageSize());
            pagination.setCurrentPage(pageQuery.getPageNo());
            result.setPagination(pagination);
            return result;
        }
        return new PageResult<>();
    }

    /**
     * 根据id获取社联干部信息
     *
     * @param id
     * @return
     */
    @Override
    public SysSauCadre findById(Integer id) {
        SysSauCadre sysSauCadre = sysSauCadreMapper.selectByPrimaryKey(id);
        if (sysSauCadre == null) {
            throw new ParamException("获取社联干部信息失败");
        }
        return sysSauCadre;
    }

    /**
     * 修改社联干部状态
     *
     * @param ids
     * @param status
     * @param username
     */
    @Override
    public void updateStatus(Integer[] ids, int status, String username) {
        Map params = new HashMap(4);
        params.put("status", status);
        params.put("lastupdTime", new Date());
        params.put("lastupdMan", username);
        sysSauCadreMapper.updateStatusByPrimaryKey(ids, params);
    }

    /**
     * 批量删除社联干部信息
     *
     * @param ids
     */
    @Override
    public void delete(Integer[] ids) {
        sysSauCadreMapper.batchDeleteByPrimaryKey(ids);
    }
}
